/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tarena.lbs.attach.rpc;

import com.tarena.lbs.attach.api.AttachApi;
import com.tarena.lbs.attach.dao.repository.AttachRepository;
import com.tarena.lbs.attach.enums.FileTypeEnum;
import com.tarena.lbs.attach.pojo.dto.AttachDTO;
import com.tarena.lbs.attach.pojo.dto.AttachQrDTO;
import com.tarena.lbs.attach.pojo.param.AttachQRParam;
import com.tarena.lbs.attach.pojo.param.PicUpdateParam;
import com.tarena.lbs.attach.pojo.po.AttachPO;
import com.tarena.lbs.attach.pojo.query.AttachQuery;
import com.tarena.lbs.attach.utils.QRCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@DubboService
@Slf4j
public  class AttachRpcService implements AttachApi {
    @Autowired
    private AttachRepository attachRepository;
    @Value("${image_path}")
    private String imagePath;
    @Value("${url")
    private String url;

    @Override
    public List<AttachDTO> getAttachInfo(AttachQuery attachQuery) {
        log.debug("根据查询参数查询图片绑定信息,入参:attachQuery={}",attachQuery);
        List<AttachPO> pos = attachRepository.selectList(attachQuery);
        List<AttachDTO> dtos = null;
        if (!pos.isEmpty()){
            dtos = new ArrayList<>();
            for (AttachPO po : pos) {
                AttachDTO dto = new AttachDTO();
                BeanUtils.copyProperties(po,dto);
                dtos.add(dto);
            }
        }
        log.debug("根据查询参数查询图片绑定信息,出参:dtos={}",dtos);
        return dtos;
    }

    @Override
    public boolean batchUpdateBusiness(List<PicUpdateParam> picParams) {
        log.info("绑定图片 执行sql之前:{}",picParams);
        int rows = attachRepository.batchUpdateAttachByIdOrFileUUID(picParams);
        log.info("绑定图片 执行sql之后:{}",rows);
        return rows>0;
    }

    @Override
    public AttachQrDTO generateQrCode(AttachQRParam attachQRParam) {
        log.info("生成二维码,入参:attachQRParam={}",attachQRParam);
        String content = attachQRParam.getContent();
        Integer businessType = attachQRParam.getBusinessType();
        Integer businessId = attachQRParam.getBusinessId();
        // 二维码UUId
        String fileUuid = UUID.randomUUID().toString().replaceAll("-", "");
        // 生成二维码文件名
        String fileName = "QR_"+businessType+"_"+businessId+"_"+fileUuid+".png";
        // 生成二维码图片
        QRCodeUtil.createCodeToFile(content, new File(imagePath), fileName);
        // 生成二维码PO，存入数据库
        AttachPO attachPO = new AttachPO();
        attachPO.setFileUuid(fileUuid);
        attachPO.setClientFileName(fileName);
        attachPO.setBusinessType(businessType);
        attachPO.setBusinessId(businessId);
        attachPO.setContentType(FileTypeEnum.PICTURE.getType());
        attachPO.setIsCover(0);
        attachPO.setStatus(1);
        attachRepository.insert(attachPO);
        // 生成二维码DTO，返回
        AttachQrDTO dto = new AttachQrDTO();
        dto.setId(attachPO.getId());
        dto.setFileUuid(fileUuid);
        dto.setUrl(url+fileName);
        log.info("生成二维码,出参:dto={}",dto);
        return dto;
    }
}
